package com.wondersgroup.fjzlpt.org.controller.rest.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.quick.framework.boot.web.utils.RequestUtils;
import com.quick.framework.util.LoggerUtil;
import com.wondersgroup.fjzlpt.org.beans.req.api.AddQyReq;
import com.wondersgroup.fjzlpt.org.beans.req.api.FinishTaskReq;
import com.wondersgroup.fjzlpt.org.beans.req.api.GetQyfyxxInfoReq;
import com.wondersgroup.fjzlpt.org.beans.req.api.GetQyxxInfoReq;
import com.wondersgroup.fjzlpt.org.beans.req.api.QueryYbfwxReq;
import com.wondersgroup.fjzlpt.org.beans.req.api.UpdateQyfyxxReq;
import com.wondersgroup.fjzlpt.org.beans.req.api.ValiDateQyxxReq;
import com.wondersgroup.fjzlpt.org.beans.resp.BaseRespBean;
import com.wondersgroup.fjzlpt.org.beans.resp.api.*;
import com.wondersgroup.fjzlpt.org.controller.rest.jtqy.JtqyRestController;
import com.wondersgroup.fjzlpt.org.service.inter.fwxx.FwxxService;
import com.wondersgroup.fjzlpt.org.service.inter.jtqy.JtqyApiService;
import com.wondersgroup.fjzlpt.org.service.inter.jtqy.JtqyService;
import com.wondersgroup.fjzlpt.org.service.inter.tdgl.TdglService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 用于第三方使用家庭签约操作
 * <p>
 * <p>
 * Created by sanma on 2017/8/29.
 */
@RestController
@RequestMapping("/api")
@Api(value = "第三方使用家庭签约操作接口" , tags = {"第三方使用家庭签约操作接口"} , description = "第三方签约，查询，付费接口")
public class DsfsyRestController {

    @Autowired
    private JtqyService jtqyService;
    @Autowired
    private JtqyApiService jtqyApiService;
    @Autowired
    private TdglService tdglService;
    @Autowired
    private FwxxService fwxxService;
    @Autowired
    private JtqyRestController jtqyRestController;

    @ApiOperation(value = "验证签约信息" , notes = "根据传入条件，验证签约数据是否可以进行签约操作" ,response = ValiDateQyxxResp.class)
    @RequestMapping(value = "/validateQyxx", method = RequestMethod.POST)
    public JSONObject validateQyxx(@RequestBody ValiDateQyxxReq requestParams ) {
        JSONObject manager = null ;
        try {
            JSONObject json = JSON.parseObject(JSON.toJSONString(requestParams));
            LoggerUtil.info("\n第三方接口validateQyxx入参如下：\n" + json.toJSONString());
            manager = jtqyApiService.validateQyxx(json);
            LoggerUtil.info("第三方接口validateQyxx校验结果：\n\n" + manager.toJSONString());
        } catch (Exception e) {
            LoggerUtil.error(e.getMessage());
            ResultObject ro = new ResultObject();
            ro.changeFaultState("接口请求参数有误！");
            manager = JSONObject.parseObject(ro.toJsonStr());
        }
        return manager;
    }

    @ApiOperation(value = "患者签约状态查询" , notes = "根据身份证号查询当前人员的签约状态，未签约的返回为签约" , response = GetQyxxInfoResp.class)
    @RequestMapping(value = "/getQyxxInfo", method = RequestMethod.POST)
    public JSONObject getQyxxInfo(@RequestBody GetQyxxInfoReq getQyxxInfoReq) {
        JSONObject manager;
        try {
//            JSONObject json = JSON.parseObject(JSON.toJSONString(getQyxxInfoReq));
            manager = jtqyApiService.getQyxxInfo(getQyxxInfoReq.getSfzh());
        } catch (Exception e) {
            LoggerUtil.error(e.getMessage());
            ResultObject ro = new ResultObject();
            ro.changeFaultState("接口请求参数有误！");
            manager = JSONObject.parseObject(ro.toJsonStr());
        }
        return manager;
    }

    @ApiOperation(value = "签约付费信息查询" , notes = "根据身份证号查询患者签约信息与服务包费用信息" , response = QyfyxxInfoResp.class)
    @RequestMapping(value = "/getQyfyxxInfo", method = RequestMethod.POST)
    public JSONObject getQyfyxxInfo(@RequestBody GetQyfyxxInfoReq getQyfyxxInfoReq) {
        JSONObject manager;
        try {
//            JSONObject json = JSON.parseObject(JSON.toJSONString(getQyfyxxInfoReq));
            manager = jtqyApiService.getQyfyxxInfo(getQyfyxxInfoReq.getSfzh());
        } catch (Exception e) {
            LoggerUtil.error(e.getMessage());
            ResultObject ro = new ResultObject();
            ro.changeFaultState("接口请求参数有误！");
            manager = JSONObject.parseObject(ro.toJsonStr());
        }
        return manager;
    }

    @ApiOperation(value = "签约付费" , notes = "签约付费信息查询后，线下付费流程完成后调用此接口完成签约收费流程。" , response = BaseRespBean.class)
    @RequestMapping(value = "/updateQyfyxx", method = RequestMethod.POST)
    public JSONObject updateQyfyxx(@RequestBody UpdateQyfyxxReq updateQyfyxxReq) {
        JSONObject manager;
        try {
            JSONObject json = JSON.parseObject(JSON.toJSONString(updateQyfyxxReq));
            json = this.jsonKeyUpper(json);//key转大写
            manager = jtqyApiService.updateQyfyxx(json);
        } catch (Exception e) {
            LoggerUtil.error(e.getMessage());
            ResultObject ro = new ResultObject();
            ro.changeFaultState("接口请求参数有误！");
            manager = JSONObject.parseObject(ro.toJsonStr());
        }
        return manager;
    }

    @RequestMapping(value = "/jtcyQyxxQuery" ,  method = RequestMethod.POST)
    @ApiIgnore
    public JSONObject jtcyQyxxQuery(HttpServletRequest request) {
        return jtqyRestController.jtcyQyxxQuery(request);
    }

    @RequestMapping(value = "/addQy" ,  method = RequestMethod.POST )
    @ApiIgnore
    public JSONObject addQy(HttpServletRequest request , AddQyReq addQyReq) {
        return jtqyRestController.addQy(request);
        //Map map = RequestUtils.GetParameterMap(request);
        //return null;
    }

    @ApiOperation(value = "查询已签订的医保服务项" , notes = "第三方查询已签订的医保服务项" , response = QueryYbfwx.class,responseContainer = "List")
    @RequestMapping(value = "/queryYbfwx" ,  method = RequestMethod.POST)
    public JSONArray queryYbfwx(HttpServletRequest request,@RequestBody QueryYbfwxReq queryYbfwxReq) {
        Map<String,String> map = new HashMap<>();
        map.put("QYXXID", queryYbfwxReq.getQyxxid());
        return jtqyService.queryYbfwx(map);
    }

    /**
     * 根据团队ID查询团队成员列表
     * @param request
     * @return
     */
    @RequestMapping("/queryTdcylbAll")
    @ApiIgnore
    public JSONArray queryTdcylbAll(HttpServletRequest request) {
        Map<String, String> map = RequestUtils.GetParameterMap(request);
        return tdglService.queryTdcylbAll(map);
    }

    /**
     * 查询所有医保服务项
     * @param request
     * @return
     */
    @RequestMapping("/queryYbFwxAll")
    @ApiIgnore
    public JSONArray queryYbFwxAll(HttpServletRequest request) {
        Map<String, String> map = RequestUtils.GetParameterMap(request);
        //ControllerCommonUtil.pageFormat(map);
        //map.put("YLJGID", QuickPrincipalUtils.getPtjgID(request));
        return fwxxService.queryAll(map);
    }

    /**
     * 查询协议服务包列表--用于打印或查询家签协议服务包价格
     * @param request
     */
    @RequestMapping("/queryXyFWB")
    @ApiIgnore
    public JSONArray downXyFwbByWord(HttpServletRequest request){
        Map<String, String> map = RequestUtils.GetParameterMap(request);
        return jtqyService.queryFwbAllByView(map);
    }

    /**
     * 查询患者已经签订的所有服务包ID
     * 	--用于前台服务包禁用
     * @return
     */
    @RequestMapping("/getYqdFwbid")
    @ApiIgnore
    public JSONObject getYqdFwbid(HttpServletRequest request){
        Map<String, String> map = RequestUtils.GetParameterMap(request);
        return jtqyService.getYqdFwbid(map);
    }

    @ApiOperation(value = "完成服务" , notes = "第三方完成签约服务" , response = BaseRespBean.class)
    @RequestMapping(value = "/finishTask" ,  method = RequestMethod.POST)
    public JSONObject finishTask(@RequestBody FinishTaskReq finishTaskReq) {
        JSONObject json = JSON.parseObject(JSON.toJSONString(finishTaskReq));
        json = this.jsonKeyUpper(json);//key转大写
        return jtqyApiService.finishTask(json);
    }

    /**
     * 单层jsonObj的key值转大写
     * @param jsonObj
     */
    private JSONObject jsonKeyUpper(JSONObject jsonObj){
        Set<String> set = jsonObj.keySet();
        JSONObject newJson = new JSONObject();
        for(String key : set){
            newJson.put(key.toUpperCase(), jsonObj.getString(key));
        }
        return newJson;
    }

}
